package sk.vasicek.drinkmanager.activity;

import java.util.Locale;

import sk.vasicek.drinkmanager.domain.Language;
import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;

public class DrinkManager extends Application {
	private Locale locale = null;

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		Configuration configClone = new Configuration(newConfig);
		if (locale != null) {
			newConfig.locale = locale;
			Locale.setDefault(locale);
			getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
		}
	}

	@Override
	public void onCreate() {
		super.onCreate();

		Configuration config = getBaseContext().getResources().getConfiguration();
		Locale locale = Language.getApplicationLanguage(this);
		Locale.setDefault(locale);
		config.locale = locale;
		getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
	}
	
	public static void setLocale(Context context) {
	    final Locale locale = Language.getApplicationLanguage(context);
	    Locale.setDefault(locale);
	    final Configuration config = new Configuration();
	    config.locale = locale;

	    final Resources res = context.getResources();
	    res.updateConfiguration(config, res.getDisplayMetrics());
	}
}
